package com.langll.ai.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.execution.ToolCallResultConverter;
import org.springframework.ai.util.json.JsonParser;
import org.springframework.lang.Nullable;

import java.lang.reflect.Type;

public class CustomToolCallResultConverter implements ToolCallResultConverter {
    private static final Logger logger = LoggerFactory.getLogger(CustomToolCallResultConverter.class);

    @Override
    public String convert(@Nullable Object result, @Nullable Type returnType) {
        System.out.println("CustomToolCallResultConverter----");
        System.out.println("result---" + result.toString());
        System.out.println("result---" + returnType.toString());
        if (returnType == Void.TYPE) {
            System.out.println("The tool has no return type. Converting to conventional response.");
            logger.debug("The tool has no return type. Converting to conventional response.");
            return "Done";
        }
        else {
            System.out.println("Converting tool result to JSON.");
            logger.debug("Converting tool result to JSON.");
            return JsonParser.toJson(result);
        }
    }
}
